package com.okcl.manage.service.impl;

import com.okcl.common.exception.ServiceException;
import com.okcl.common.utils.DateUtils;
import com.okcl.manage.domain.Book;
import com.okcl.manage.domain.BorrowRecord;
import com.okcl.manage.domain.Category;
import com.okcl.manage.domain.dto.BorrowRecordDto;
import com.okcl.manage.mapper.BookMapper;
import com.okcl.manage.mapper.HomeMapper;
import com.okcl.manage.mapper.UserInfoMapper;
import com.okcl.manage.service.IHomeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@RequiredArgsConstructor
public class HomeServiceImpl implements IHomeService {
    private final HomeMapper homeMapper;
    private final BookMapper bookMapper;
    private final UserInfoMapper userInfoMapper;

    /**
     * 查询类别信息
     *
     * @return
     */
    @Override
    public List<Category> getCateGoryInfoList() {
        return homeMapper.getCateGoryInfoList();
    }

    /**
     * 获取图书信息
     *
     * @return
     */
    @Override
    public List<Book> getBookInfoList() {
        return homeMapper.getBookInfoList();
    }

    /**
     * 提交借阅记录
     *
     * @param borrowRecordDto
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int submitBorrowRecord(BorrowRecordDto borrowRecordDto) {
        Long userId = borrowRecordDto.getUserId();
        if (userInfoMapper.getUserNameById(userId) == null) {
            throw new ServiceException("用户不存在");
        }
        List<Long> bookIds = borrowRecordDto.getBooks();
        List<String> bookIsNuList = new ArrayList<>();
        bookIds.forEach(bookId -> {
            Book book = bookMapper.selectBookById(bookId);
            Long quantity = book.getQuantity();
            //创建一个集合用于记录库存为空的图书集合
            if (quantity <= 0) {
                bookIsNuList.add(book.getBookName());
            }
        });
        if (!bookIsNuList.isEmpty()) {
            throw new ServiceException("库存不足，无法借阅：" + bookIsNuList);
        }
        //创建一个借阅记录对象集合
        List<BorrowRecord> bookBorrowRecordList = new ArrayList<>();
        borrowRecordDto.getBooks().forEach(bookId -> {
            BorrowRecord borrowRecord = new BorrowRecord();
            borrowRecord.setBorrowDate(borrowRecordDto.getBorrowDate());
            borrowRecord.setUserId(borrowRecordDto.getUserId());
            borrowRecord.setBookId(bookId);
            //获取当前日期
            Date now = new Date();
            //现在日期往后推30天
            borrowRecord.setDueDate(DateUtils.addDays(now, 30));
            bookBorrowRecordList.add(borrowRecord);
            //同时修改对应的库存信息
            Book book = new Book();
            book.setId(bookId);
            Book book1 = bookMapper.selectBookById(bookId);//根据id查询得到的对象
            book.setQuantity(book1.getQuantity() - 1);
            bookMapper.updateBook(book);
        });
        int i = homeMapper.submitBorrowRecord(bookBorrowRecordList);
        return i;
    }
}
